package com.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.springcloud.pojo.Dept;
import com.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ProjectName: springcloud_day1
 * @Package: com.springcloud.controller
 * @ClassName: DeptHystrixController
 * @Author: 郑涛
 * @Description:  断路器  服务熔断
 * @Date: 2020/10/24 9:46
 * @Version: 1.0
 */
@RestController
public class DeptHystrixController {


    @Autowired
    DeptService deptService;
    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod="HystrixgetByid")   //如果getById 调用失败 就会调用HystrixgetByid这个备用方法
    public Dept getById(@PathVariable("id") Long id) {
        Dept dept = new Dept();
        dept = deptService.getById(id);
        if(dept ==null){
            throw  new RuntimeException("id====>"+id+",不存在对应信息");
        }
        System.out.println(dept);
        return dept;
    }

    /**
     * 备用方法
     * @param id
     * @return
     */
    private  Dept HystrixgetByid(@PathVariable("id") Long id) {
        return new Dept().setDeptno(id).setDname("暂无信息").setDb_source(null);
    }
}
